#ifdef WX_PRECOMP
    #include "wx_pch.h"
#else
#endif

#include <wx/fileconf.h>
#include <wx/textctrl.h>
#include <wx/filedlg.h>
#include <wx/dirdlg.h>

#include "configmanager.h"

#include "codesnippetswindow.h"
#include "snippetsconfig.h"
#include "settingsdlg.h"
#include "settingsdlgform.h"
#include "version.h"

   BEGIN_EVENT_TABLE(SettingsDlg, SettingsDlgForm)
    EVT_BUTTON(wxID_OK,            SettingsDlg::OnOk)
    EVT_BUTTON(ID_EXTEDITORBUTTON, SettingsDlg::OnExtEditorButton)
    EVT_BUTTON(ID_SNIPPETFILEBUTTON, SettingsDlg::OnSnippetFolderButton)
   END_EVENT_TABLE()

SettingsDlg::SettingsDlg(wxWindow* parent)
    :SettingsDlgForm(parent ,-1,wxT("User Settings")) {
   
   
   
    GetConfig()->CenterChildOnParent(this);

   
    m_ExtEditorTextCtrl-> SetValue( wxT("Enter filename of external editor") );
    m_SnippetFileTextCtrl->SetValue(wxT("Enter Snippets storage Folder") );


    if ( not GetConfig()->SettingsExternalEditor.IsEmpty() )
        m_ExtEditorTextCtrl-> SetValue( GetConfig()->SettingsExternalEditor );


    if ( not GetConfig()->SettingsSnippetsFolder.IsEmpty() )
        m_SnippetFileTextCtrl-> SetValue( GetConfig()->SettingsSnippetsFolder );

   
    m_ToolTipsChkBox->SetValue( GetConfig()->GetToolTipsOption() );

   
    wxString windowState = GetConfig()->GetSettingsWindowState();


   
   
   
   
    m_CfgFolderTextCtrl->SetValue( GetConfig()->SettingsCBConfigPath );
    m_IniFolderTextCtrl->SetValue( GetConfig()->SettingsSnippetsCfgPath );
}

SettingsDlg::~SettingsDlg() {
   
}

void SettingsDlg::OnOk(wxCommandEvent& event) {
    wxString filename = m_ExtEditorTextCtrl->GetValue();
    if ( not filename.IsEmpty() )
        GetConfig()->SettingsExternalEditor = filename;
    else
        GetConfig()->SettingsExternalEditor = wxEmptyString;

    filename = m_SnippetFileTextCtrl->GetValue();
    if ( not filename.IsEmpty() )
        GetConfig()->SettingsSnippetsFolder = filename;
    else
        GetConfig()->SettingsSnippetsFolder = wxEmptyString;

   
    GetConfig()->SetToolTipsOption( m_ToolTipsChkBox->GetValue() );

    wxString windowState = wxT("Floating");

    GetConfig()->SetSettingsWindowState( windowState) ;

    this->EndModal(wxID_OK);
    LOGIT( _T("OnOK Saving Settings"));
    GetConfig()->SettingsSave();
}

void SettingsDlg::OnExtEditorButton(wxCommandEvent& event) {
   
    wxString newFileName;
    GetFileName(newFileName);

    if (not newFileName.IsEmpty())
        m_ExtEditorTextCtrl-> SetValue( newFileName );
}

void SettingsDlg::OnSnippetFolderButton(wxCommandEvent& event) {
   
    wxString newFolderName;
    newFolderName = AskForPathName();

    if (not newFolderName.IsEmpty())
        m_SnippetFileTextCtrl-> SetValue( newFolderName) ;
}

void SettingsDlg::GetFileName(wxString& newFileName) {
    newFileName = wxEmptyString;

   
    wxFileDialog dlg(this,                          
                 _T("Select file "),                
                 wxEmptyString,                     
                 wxEmptyString,                     
                 wxT("*.*"),                        
                 wxFD_OPEN | wxFD_FILE_MUST_EXIST );

  
    wxPoint mousePosn = ::wxGetMousePosition();
    (&dlg)->Move(mousePosn.x, mousePosn.y);

    if (dlg.ShowModal() != wxID_OK) return;
    newFileName = dlg.GetPath();

    #ifdef LOGGING
     LOGIT( _T("New filename[%s]"), newFileName.GetData() );
    #endif

}

wxString SettingsDlg::AskForPathName()      
{
    wxString newPathName = wxEmptyString;

   
    wxDirDialog dlg(::wxGetTopLevelParent(0),  
                 _T("Select path "),            
                 ::wxGetCwd(),                 
                 wxDD_DEFAULT_STYLE );         

  
    wxPoint mousePosn = ::wxGetMousePosition();
    (&dlg)->Move(mousePosn.x, mousePosn.y);

    if (dlg.ShowModal() != wxID_OK) return wxEmptyString;
    return newPathName = dlg.GetPath();
}
